$(function() {
    getuserinfo();

    $('#logout').on('click', function() {
        layui.layer.confirm('确定要退出吗？', { icon: 3, title: '提示' }, function(index) {
            localStorage.removeItem('token');
            location.href = '/login.html';
            layer.close(index);
        })
    })
});

function getuserinfo() {
    $.ajax({
        type: 'GET',
        url: '/my/userinfo',
        success: function(res) {
            if (res.status != 0) {
                return layui.layer.msg('身份信息获取失败！')
            } else {
                getavatar(res.data)
            }
        }
    })
}

function getavatar(user) {
    var name = user.nickname || user.username;
    $('.welcome').html('欢迎！&nbsp' + name);
    if (user.user_pic != null) {
        $('.layui-nav-img').attr('src', user.user_pic).show()
        $('.new-avatar').hide();
    } else {
        var arr = name.split('');
        var first = arr[0].toUpperCase();
        $('.new-avatar').html(first).show();
        $('.layui-nav-img').hide()
    }
}